<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Money extends Auth_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('money_mdl');
        $this->load->model('validate_mdl');
    }

    /**
     * 用户查看实际结算价格
     */
    public function index(){
        if($this->session->group != 3){
            show_error('没有权限！');
        }
        $password = $this->input->post('password');
        if(!empty($password)){
            if(!$this->input->is_ajax_request()){
                show_error('请求方法出错！');
            }
            //Model
            $this->load->model('users_mdl');

            $password = md5($password);
            $this->db->where('id',$_SESSION['user']['id']);
            $this->db->where('password',$password);
            $res = $this->users_mdl->get_one();
            if(empty($res)){
                die('密码错误！');
            }
            $mess = $res['id'].$_SERVER['REQUEST_TIME'].rand(1,999);
            $mess = md5($mess);
            $data = array(
                'validate' => substr($mess,5,20),
                'user_id' => $res['id'],
                'status' => '1',
                'create_time' => $_SERVER['REQUEST_TIME'],
                'create_date' => date('Y-m-d H:i:s',$_SERVER['REQUEST_TIME'])
            );
            $r = $this->validate_mdl->create($data);
            echo $data['validate'];
        }else{
            $this->_view('index');
        }
    }

    /**
     * 查看用户退还信息
     */
    public function show(){
        if($this->session->group != 3){
            show_error('没有权限！');
        }

        $val = $this->input->get('val');
        if(empty($val)){
            show_error('参数错误！');
        }

        $this->db->where('validate',$val);
        $this->db->where('user_id',$_SESSION['user']['id']);
        $this->db->where('create_time >',$_SERVER['REQUEST_TIME'] - 600);
        $this->db->where('status','1');

        $res = $this->validate_mdl->get_one();
        if(empty($res)){
           redirect(site_url('money/index'));
        }else{
            $this->db->where('create_time <',$_SERVER['REQUEST_TIME'] - 600);
            $this->db->set('status','2');
            $this->validate_mdl->update();
        }

        $this->db->start_cache();
        $this->db->where('user_id',$_SESSION['user']['id']);
        $this->db->stop_cache();

        //查询用户返款记录
        $this->load->library('pagination');

        $config['base_url'] = site_url('money/show?val='.$val);
        $config['total_rows'] = $this->money_mdl->get_count();
        $config['per_page'] = 10;
        $config['page_query_string'] = TRUE;
        $config['first_link'] = '首页';
        $config['last_link'] = '尾页';
        $config['next_link'] = '下一页';
        $config['prev_link'] = '上一页';
        $config['anchor_class'] = 'current';

        $this->pagination->initialize($config);
        $data['page'] = $this->pagination->create_links();

        $per_page = $this->input->get('per_page');
        $this->db->limit($config['per_page'],intval($per_page));
        $this->db->order_by('create_time desc');
        $list = $this->money_mdl->select();
        $this->db->flush_cache();

        //Mondel
        $this->load->model('order_mdl');
        $this->load->model('parameters_mdl','par_mdl');
        $this->load->model('project_mdl');

        foreach($list as $k => $v){
            //查询订单详情
            $this->db->where('id',$v['order_id']);
            $this->db->select('parameters_id,price, remarks,status,user_name');
            $list[$k]['order'] = $this->order_mdl->get_one();
            //查询订单状态
            $status = $this->order_mdl->status;
            $list[$k]['sorder'] = $status[$list[$k]['order']['status']];
            //查询项目名称
            $this->db->where('id',$list[$k]['order']['parameters_id']);
            $list[$k]['par'] = $this->par_mdl->get_one();
            //查询项目名称
            $this->db->where('id',$list[$k]['par']['project_id']);
            $list[$k]['project'] = $this->project_mdl->get_one();
        }

        //查询所有可领取余额
        $this->db->where('user_id',$_SESSION['user']['id']);
        $this->db->where('status','1');
        $this->db->select('sum(balance) as balance');
        $count = $this->money_mdl->get_one();
        $data['balance'] = $count['balance'];
        $data['list'] = $list;
        $this->_view('show',$data);
    }

    /**
     * 转入余额
     */
    public function to_balance(){
        if(!$this->input->is_ajax_request()){
            show_error('参数错误！');
        }

        //查询所有可领取余额
        $this->db->where('user_id',$_SESSION['user']['id']);
        $this->db->where('status','1');
        $this->db->select('sum(balance) as balance');
        $count = $this->money_mdl->get_one();

        if($count['balance'] < 1){
            die('没有可转入余额！');
        }

        $this->load->model('uinfo_mdl');
        $this->load->model('bill_mdl');
        $this->load->model('mess_mdl');

        $this->db->trans_start();
        //更改状态
        $this->db->where('user_id',$_SESSION['user']['id']);
        $this->db->where('status','1');
        $this->db->set('status','3');
        $this->money_mdl->update();
        //写入余额
        $this->db->where('id',$_SESSION['user']['id']);
        $this->db->set('money','money+'.$count['balance'],FALSE);
        $this->uinfo_mdl->update();
        //写入账单
        $this->bill_mdl->add_bill($_SESSION['user']['id'],$count['balance'],"可用资金转入");
        //通知消息
        $mess = "尊敬的用户，您好， 您的可转入资金".$count['balance']."元已经成功转入您的余额中。";
        $this->mess_mdl->send_message($mess);

        //判断充值资金是否满足升级条件
        $this->load->model('users_mdl');
        $this->users_mdl->set_group($data['user_id']);

        $this->db->trans_complete();

        echo 1;
    }

}

/* End of file Money.php */
/* Location: ./application/controllers/Money.php */